package com.deco.service.sys;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.deco.dao.domain.table.CmsAdv;
import com.deco.domain.vo.sys.CmsAdvTypeVo;
import com.deco.domain.vo.sys.CmsAdvVo;
import com.deco.logic.sys.CmsAdvLogic;
import com.deco.logic.sys.CmsAdvTypeLogic;
import com.deco.service.CommonService;
import com.free.common.vo.PageResponseVo;
import com.free.common.vo.PageSearchVo;
import com.free.common.vo.ResultModel;

@Service
public class CmsAdvService extends CommonService {
	   /*========================================================================*
     *                         Public Fields (公共属性)
     *========================================================================*/

    /*========================================================================*
     *                         Private Fields (私有属性)
     *========================================================================*/
    
    @Autowired
    private CmsAdvLogic cmsAdvLogic;
    
    @Autowired
    private CmsAdvTypeLogic cmsAdvTypelogic;
    /*========================================================================*
     *                         Construct Methods (构造方法)
     *========================================================================*/

    /*========================================================================*
     *                         Public Methods (公有方法)
     *========================================================================*/
    public List<CmsAdvVo> getCmsAdvVoList(CmsAdvVo vo, String orderBy){
		return cmsAdvLogic.list(vo, orderBy);
	}
    
    public CmsAdvVo getCmsAdv(int id){
    	return cmsAdvLogic.getCmsAdv(id);
    }
    
    public ResultModel save(CmsAdvVo cmsAdvVo){
        Timestamp nowTime = new Timestamp(System.currentTimeMillis());
    	if(cmsAdvVo.getId() == null || cmsAdvVo.getId() == 0L){
    		cmsAdvVo.setCreateTime(nowTime);
    		cmsAdvVo.setUpdateTime(nowTime);
    		cmsAdvLogic.create(cmsAdvVo);
    	}
    	else{
    		cmsAdvVo.setUpdateTime(nowTime);
    		cmsAdvLogic.update(cmsAdvVo);
    	}
    	return ResultModel.success();
    }

	public ResultModel delete(int id) {
		cmsAdvLogic.delete(id);
	  	return ResultModel.success();
	}

	public ResultModel batchDelete(Integer[] ids) {
		if(ids == null || ids.length == 0){
    		return ResultModel.success();
		}
    	int res = cmsAdvLogic.batchDelete(ids);
    	if(res > 0){
    		return ResultModel.success();
		}
		return ResultModel.failure();
	}

	public PageResponseVo<CmsAdvVo> pageInfo(PageSearchVo<CmsAdvVo> condition) {
		 PageResponseVo<CmsAdvVo> responVo = cmsAdvLogic.getPageInfo(condition);
		 List<CmsAdvVo> cmsAdvList = responVo.getList();
		 if(cmsAdvList != null && cmsAdvList.size() > 0){
			 List<CmsAdvTypeVo> advTypeList = cmsAdvTypelogic.getAll();
			 if(advTypeList != null && cmsAdvList.size() > 0){
				 for(CmsAdvVo cmsAdvVo : cmsAdvList){
					 for(CmsAdvTypeVo cmsAdvTypeVo: advTypeList){
						 if(cmsAdvVo.getTypeId() == cmsAdvTypeVo.getId()){
							 cmsAdvVo.setTypeFmt(cmsAdvTypeVo.getName());
						 }
					 }
				 }
			 }
			
		 }
		 return responVo;
	}
	
	public List<CmsAdvVo> getAll() {
		return cmsAdvLogic.getAll();
	}

    /*========================================================================*
     *                         Private Methods (私有方法)
     *========================================================================*/

}
